package com.yyk.yiliao.ui.shop.fragment;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.bumptech.glide.Glide;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseFragment;
import com.yyk.yiliao.adapter.CommonTabPagerAdapter;
import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.ui.activity.detail.activity.Detail_Activity;
import com.yyk.yiliao.ui.activity.detail.activity.ShAdressGl_Activity;
import com.yyk.yiliao.ui.activity.dingdan.activity.CommodityOrde_Activity;
import com.yyk.yiliao.ui.activity.shopcart.ShopCart_Activity;
import com.yyk.yiliao.ui.login.activity.LogIn_Activity;
import com.yyk.yiliao.ui.shop.activity.AnZhengZhaoYao_Activity;
import com.yyk.yiliao.ui.shop.activity.Hospital_Search_Activity;
import com.yyk.yiliao.util.CheckDecimalPointUtil;
import com.yyk.yiliao.util.GlideImageLoader;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.ebs.AzzyEvent;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.HomeHomemiddle_Info;
import com.yyk.yiliao.bean.OrderCartCountkey_Info;
import com.yyk.yiliao.bean.ShopBrand_Info;
import com.yyk.yiliao.bean.ShopGoodstype_Info;
import com.yyk.yiliao.bean.ShopRecommend_Info;
import com.yyk.yiliao.bean.ShopTodaydeal_Info;
import com.yyk.yiliao.bean.StoreCustomclass_Info;
import com.yyk.yiliao.widget.diver.more.SpacesItemDecoration;
import com.yyk.yiliao.widget.paowuxian.BazierAnimView;

import org.greenrobot.eventbus.EventBus;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.HttpException;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ${ZKbilian} on 2018/7/18 0018.
 */

public class ShoppingMall_Fragment extends BaseFragment {
    public static ShoppingMall_Fragment intanse;
    @BindView(R.id.nHospital_vp)
    Banner nHospitalVp;
    @BindView(R.id.nHospital_today)
    RecyclerView nHospitalToday;
    @BindView(R.id.nHospital_ppzx)
    RecyclerView nHospitalPpzx;

    @BindView(R.id.nHospital_fvp)
    ViewPager nHospitalFvp;


    @BindView(R.id.tabLayout)
    XTabLayout tabLayout;
    @BindView(R.id.nHospital_wddd) //我的订单对话框
            LinearLayout nHospitalWddd;


    @BindView(R.id.img_one)
    ImageView imgOne;
    @BindView(R.id.img_two)
    ImageView imgTwo;
    @BindView(R.id.img_three)
    ImageView imgThree;
    @BindView(R.id.img_four)
    ImageView imgFour;
    @BindView(R.id.img_five)
    ImageView imgFive;
    @BindView(R.id.img_six)
    ImageView imgSix;

    @BindView(R.id.iv_tu)
    ImageView ivTu;
    @BindView(R.id.iv_tu2)
    ImageView ivTu2;
    @BindView(R.id.iv_tu3)
    ImageView ivTu3;
    @BindView(R.id.nHospital_rv_icon)
    RecyclerView nHospitalRvIcon;
    @BindView(R.id.nHome_search)
    TextView nHomeSearch;

    @BindView(R.id.nHome_location_tv)
    TextView nHomeLocationTv;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    //抛物线
    @BindView(R.id.cornerimg) //购物车图标
            ImageView cornerImg;
    @BindView(R.id.unread_msg_number)//购物车图标数量
            TextView msgNuber;
    @BindView(R.id.bezier_anim)
    BazierAnimView bazierAnimView;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;
    private int dataNumber = 1;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_shoppingmall;
    }

    @Override
    public void initView() {
        initToolbar();
        initRefresh();
    }

    private void initRefresh() {
        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        initPost();
                        //禁止点击内容
                        smartrefreshlayout.setDisableContentWhenRefresh(true);
                        smartrefreshlayout.finishRefresh();
                    }
                }, 1500);

            }
        });
    }

    /**
     * Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理 - 圣骑士wind - 博客园  https://www.cnblogs.com/mengdd/p/5590634.html
     */
    private void initToolbar() {
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        setHasOptionsMenu(true);
        //添加menu项点击事件
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.toolbar_r_img:
                        Log.e("Test---->", "点击了右边图标");
                        break;
                    case R.id.toolbar_r_1:
                        Log.e("Test---->", "点击了弹出菜单1");
                        startAct(CommodityOrde_Activity.class);
                        break;
                    case R.id.toolbar_r_2:
                        Log.e("Test---->", "点击了弹出菜单2");
                        break;
                    case R.id.toolbar_r_3:
                        Log.e("Test---->", "点击了弹出菜单3");
                        startAct(ShAdressGl_Activity.class);
                        break;
                }
                return true;    //返回为true
            }
        });
    }

    private static final int START_ITEM = Menu.FIRST;  //Menu.FIRST的值就是1
    private static final int OVER_ITEM = Menu.FIRST + 1;
    private static final int SET_ITEM1 = Menu.FIRST + 2;
    private static final int SET_ITEM2 = Menu.FIRST + 3;

    //设置menu（右边图标）
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        getActivity().getMenuInflater().inflate(R.menu.shop_menu, menu);

    }

    @Override
    public void initData() {
        intanse = this;
        initGetIntent();
        initPost();
        initAdapter();

    }

    private void initAdapter() {
    }

    /**
     * 设置定位
     */
    private void initGetIntent() {
        String poiName = Hawk.get("poiName");
        nHomeLocationTv.setText(poiName);
    }


    /**
     * 1.Banner图
     * 2. 头部icon
     */
    private void initPost() {
        setBanner(nHospitalVp);
        setIcon(null);
        setToady(null);
        setPpzx(null);
        setTabCommodity(nHospitalFvp, tabLayout, getChildFragmentManager());
        setHomeHomemiddle(); //推荐图
        setHomeHomemiddle2();
        setHomeHomemiddle3();
        setHomeHomemiddle4();
        initShopNumber(); //购物车数量
    }

    @Override
    public void onResume() {
        super.onResume();
        //刷新购物车数量
        initShopNumber();
    }

    private void onFailure(String msg) {

    }

    public void onComplete() {

    }

    private void onSuccess(Object info) {
        if (info instanceof ShopTodaydeal_Info) {
            //            Logger.i("今日特价:" + info.toString());
            final List<ShopTodaydeal_Info.DataBean> data = ((ShopTodaydeal_Info) info).getData();
            BaseRecyclerAdapter<ShopTodaydeal_Info.DataBean> adapter = new BaseRecyclerAdapter<ShopTodaydeal_Info.DataBean>(getActivity(), data, R.layout.adapter_item_hospital_today) {
                @Override
                public void convert(BaseRecyclerHolder holder, ShopTodaydeal_Info.DataBean item, int position, boolean isScrolling) {
                    holder.setText(R.id.b_hospital_name, item.getName());
                    //float  整数留两个0
                    String money = CheckDecimalPointUtil.checkPoint(item.getMoney());
                    holder.setText(R.id.b_money, "¥ " + money);
                    //设置图片
                    holder.setImageByUrl(R.id.b_picture, ApiService.BASE_URL + item.getPic());

                }
            };

            //            adapter.setLength(6);
            nHospitalToday.setLayoutManager(new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false));
            nHospitalToday.addItemDecoration(new SpacesItemDecoration(14, 0));
            nHospitalToday.setAdapter(adapter);
            adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView parent, View view, int position) {
                    //商品ID
//                    Intent intent = new Intent(getActivity(), Detail_Activity.class);
//                    int id = shopList.get(position).getId();
//                    EventBus.getDefault().postSticky(new MessageEvent(id + ""));
//                    startActivity(intent);
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", data.get(position).getId());
                    startAct(Detail_Activity.class, bundle);
                    //                    Logger.e(shopList.get(position).getId() + "");
                }
            });


        }
        if (info instanceof ShopRecommend_Info) {
            //            Logger.i("商城首页商品推荐:" + info.toString());
            //            ImageView img[] = {ivTu, ivTu2, ivTu3};
            //            ImageView img[] = {imgOne, imgTwo, imgThree, imgFour, imgFive, imgSix};
            ImageView img[] = {imgOne, imgTwo, imgThree, imgFour, imgFive, imgSix};
            final List<ShopRecommend_Info.DataBean> data = ((ShopRecommend_Info) info).getData();
            for (int i = 0; i < data.size(); i++) {
                Glide.with(getContext()).load(ApiService.BASE_URL + data.get(i).getPic()).into(img[i]);
            }
        }

        if (info instanceof ShopBrand_Info) {
            //            Logger.i("品牌直销:" + info.toString());
            final List<ShopBrand_Info.DataBean> data = ((ShopBrand_Info) info).getData();
            BaseRecyclerAdapter<ShopBrand_Info.DataBean> adapter = new BaseRecyclerAdapter<ShopBrand_Info.DataBean>(getActivity(), data, R.layout.adapter_item_hospital_ppzx) {
                @Override
                public void convert(BaseRecyclerHolder holder, ShopBrand_Info.DataBean item, int position, boolean isScrolling) {
                    holder.setText(R.id.b_hospital_name, item.getName());
                    //float  整数留两个0
                    String money = CheckDecimalPointUtil.checkPoint(item.getMoney());
                    holder.setText(R.id.b_money, "¥ " + money);
                    holder.setText(R.id.b_drname, item.getDrname());
                    //设置图片
                    holder.setImageByUrl(R.id.b_picture, ApiService.BASE_URL + item.getPic());
                }
            };
            nHospitalPpzx.setLayoutManager(new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false));
            nHospitalPpzx.addItemDecoration(new SpacesItemDecoration(12, 0));
            nHospitalPpzx.setAdapter(adapter);
            adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView parent, View view, int position) {
                    //商品ID
//                    Intent intent = new Intent(getActivity(), Detail_Activity.class);
//                    int id = shopList.get(position).getId();
//                    EventBus.getDefault().postSticky(new MessageEvent(id + ""));
//                    startActivity(intent);

                    Bundle bundle = new Bundle();
                    bundle.putInt("id", data.get(position).getId());
                    startAct(Detail_Activity.class, bundle);
                    //                    Logger.e(shopList.get(position).getId() + "");
                }
            });
        }
        if (info instanceof ShopGoodstype_Info) {
            //            //底部新闻列表
            final List<ShopGoodstype_Info.DataBean> data = ((ShopGoodstype_Info) info).getData();
            final List<String> string = new ArrayList<>();
            final List<String> integers = new ArrayList<>();
            string.add("推荐");
            integers.add("-1");
            for (int i = 0; i < data.size(); i++) {
                string.add(data.get(i).getName());
                integers.add(data.get(i).getCustomid() + "");
            }

            Logger.e("zk" + string.toString());
            Logger.e("zk" + integers.toString());
            CommonTabPagerAdapter adapter = new CommonTabPagerAdapter(getChildFragmentManager()
                    , string.size(), string, getActivity());
            adapter.setListener(new CommonTabPagerAdapter.TabPagerListener() {
                @Override
                public Fragment getFragment(int position) {
                    ShopTab_Fragment fragment = ShopTab_Fragment.newInstance(position);
                    Bundle bundle = new Bundle();
                    bundle.putString("hha", integers.get(position) + "");
                    fragment.setArguments(bundle);
                    return fragment;
                }
            });
            nHospitalFvp.setAdapter(adapter);
            tabLayout.setupWithViewPager(nHospitalFvp);
            nHospitalFvp.setCurrentItem(0);


        }
        if (info instanceof StoreCustomclass_Info) {
            Logger.i("商城 图标:" + info.toString());
            final List<StoreCustomclass_Info.DataBean> data = ((StoreCustomclass_Info) info).getData();
            BaseRecyclerAdapter<StoreCustomclass_Info.DataBean> adapter = new BaseRecyclerAdapter<StoreCustomclass_Info.DataBean>(getActivity(), data, R.layout.adapter_azzy_gridview_tv_img) {
                @Override
                public void convert(BaseRecyclerHolder holder, StoreCustomclass_Info.DataBean item, int position, boolean isScrolling) {
                    holder.setText(R.id.tv, item.getName());
                    holder.setImageByUrl(R.id.img, ApiService.BASE_URL + item.getIcon());
                }
            };

            //            adapter.setLength(11);
            nHospitalRvIcon.setLayoutManager(new GridLayoutManager(getContext(), 5, GridLayoutManager.VERTICAL, false));
            nHospitalRvIcon.addItemDecoration(new SpacesItemDecoration(14, 28));
            nHospitalRvIcon.setAdapter(adapter);
            adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView parent, View view, int position) {
                    Intent intent = new Intent(getContext(), AnZhengZhaoYao_Activity.class);
                    EventBus.getDefault().postSticky(new AzzyEvent(data.get(position).getId() + "", position + ""));
                    startActivity(intent);
                }
            });

        }

    }

    /**
     * 刷新购物车数量
     */
    public void reshefeshShopNumber(final LinearLayout buy, final String pic) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                bazierAnimView.startCartAnim(buy, cornerImg, R.layout.l_move, pic);
                initShopNumber();
            }
        });

    }

    /**
     * 购物车数量
     */
    public void initShopNumber() {
        Map<String, String> map = new TreeMap<>();

        map.put("uid", Hawk.get("uid", 0) + "");

        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postCarCountkey(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<OrderCartCountkey_Info>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
//                        ToastUtil.showShort(getActivity(), "商城异常");
                    }

                    @Override
                    public void onNext(OrderCartCountkey_Info info) {
                        Logger.w("购物车数量" + info.toString());
                        if (info.getCode() == 1) {
                            dataNumber = info.getGoodstypes();
                            if (dataNumber == 0) {
                                msgNuber.setVisibility(View.INVISIBLE);
                            } else {
                                msgNuber.setText(dataNumber + "");
                                msgNuber.setVisibility(View.VISIBLE);

                            }

                        }
                    }
                });

    }

    /**
     * 商城首页商品推荐
     * 患者端 _商城_icon_下方第一栏
     */

    public void setHomeHomemiddle() {
        Map<String, String> map = new TreeMap<>();
        map.put("typeid", 8 + "");

        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postHomeHomemiddle(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeHomemiddle_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        //失败的时候调用-----一下可以忽略 直接 callBack.onFaild("请求失败");
                        Logger.e("MyFind" + e.getMessage());

                    }

                    @Override
                    public void onNext(HomeHomemiddle_Info info) {
                        if (info.getCode() == 1) {
                            String pic = info.getData().get(0).getPic();
                            String pic1 = info.getData().get(1).getPic();
                            Glide.with(getContext()).load(ApiService.BASE_URL + pic).into(ivTu);
                            Glide.with(getContext()).load(ApiService.BASE_URL + pic1).into(ivTu2);
                        } else {

                        }
                    }


                });

    }

    /**
     * 商城首页商品推荐
     * 患者端 _商城_icon_下方第二栏
     */

    public void setHomeHomemiddle2() {
        Map<String, String> map = new TreeMap<>();
        map.put("typeid", 9 + "");
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postHomeHomemiddle(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeHomemiddle_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        //失败的时候调用-----一下可以忽略 直接 callBack.onFaild("请求失败");
                        Logger.e("MyFind" + e.getMessage());
                    }

                    @Override
                    public void onNext(HomeHomemiddle_Info info) {
                        if (info.getCode() == 1) {
                            String pic = info.getData().get(0).getPic();
                            Glide.with(getContext()).load(ApiService.BASE_URL + pic).into(ivTu3);
                        } else {

                        }
                    }


                });

    }

    /**
     * 商城首页商品推荐
     * 患者端 _商城_今日特价底部广告栏_下方第一栏
     */

    public void setHomeHomemiddle3() {
        Map<String, String> map = new TreeMap<>();
        map.put("typeid", 6 + "");

        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postHomeHomemiddle(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeHomemiddle_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        //失败的时候调用-----一下可以忽略 直接 callBack.onFaild("请求失败");
                        Logger.e("MyFind" + e.getMessage());
                    }

                    @Override
                    public void onNext(HomeHomemiddle_Info info) {
                        if (info.getCode() == 1) {
                            String pic = info.getData().get(0).getPic();
                            String pic1 = info.getData().get(1).getPic();
                            Glide.with(getContext()).load(ApiService.BASE_URL + pic).into(imgOne);
                            Glide.with(getContext()).load(ApiService.BASE_URL + pic1).into(imgTwo);
                        } else {

                        }
                    }
                });
    }

    /**
     * 商城首页商品推荐
     * 患者端 _商城_今日特价底部广告栏_下方第一栏
     */

    public void setHomeHomemiddle4() {
        Map<String, String> map = new TreeMap<>();
        map.put("typeid", 7 + "");

        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postHomeHomemiddle(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeHomemiddle_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        //失败的时候调用-----一下可以忽略 直接 callBack.onFaild("请求失败");
                        Logger.e("MyFind" + e.getMessage());
                    }

                    @Override
                    public void onNext(HomeHomemiddle_Info info) {
                        if (info.getCode() == 1) {
                            String pic = info.getData().get(0).getPic();
                            String pic1 = info.getData().get(1).getPic();
                            String pic2 = info.getData().get(2).getPic();
                            String pic3 = info.getData().get(3).getPic();
                            Glide.with(getContext()).load(ApiService.BASE_URL + pic).into(imgThree);
                            Glide.with(getContext()).load(ApiService.BASE_URL + pic1).into(imgFour);
                            Glide.with(getContext()).load(ApiService.BASE_URL + pic2).into(imgFive);
                            Glide.with(getContext()).load(ApiService.BASE_URL + pic3).into(imgSix);
                        } else {

                        }
                    }


                });

    }

    /**
     * 设置banner
     *
     * @param vp
     */
    private void setBanner(Banner vp) {
        List<Integer> images2 = new ArrayList<>();
        images2.add(R.mipmap.bannert2x);
        images2.add(R.mipmap.banner2x2);

        vp.setImages(images2);
        //设置图片加载器
        vp.setImageLoader(new GlideImageLoader());
        //设置图片集合
        //banner设置方法全部调用完毕时最后调用
        vp.start();
    }

    /**
     * 今日特价
     *
     * @param parms
     */
    public void setToady(String parms) {
        Map<String, String> map = new TreeMap<>();
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postShopTodaydeal(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<ShopTodaydeal_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                        onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        //失败的时候调用-----一下可以忽略 直接 callBack.onFaild("请求失败");
                        if (e instanceof HttpException) {
                            HttpException httpException = (HttpException) e;
                            //httpException.response().errorBody().string()
                            int code = httpException.code();
                            if (code == 500 || code == 404) {
                                onFailure("服务器出错");
                            }
                        } else if (e instanceof ConnectException) {
                            onFailure("网络断开,请打开网络!");
                        } else if (e instanceof SocketTimeoutException) {
                            onFailure("网络连接超时!!");
                        } else {
                            onFailure("发生未知错误" + e.getMessage());
//                            Logger.e("MyFind", e.getMessage());
                        }

                    }

                    @Override
                    public void onNext(ShopTodaydeal_Info info) {
                        if (info.getCode() == 1) {
                            onSuccess(info);//成功------获取完数据,返回给P---P获取到数据之后就将数据交回给V
                        } else {
                            onFailure("数据为空");
                        }
                    }


                });
        try {
            //因为网络请求在子线程，所以不能让宿主线程过早的结束。
            Thread.sleep(100);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 品牌直销
     *
     * @param parms
     */
    private void setPpzx(String parms) {
        Map<String, String> map = new TreeMap<>();

        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postShopBrand(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<ShopBrand_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                        onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        //失败的时候调用-----一下可以忽略 直接 callBack.onFaild("请求失败");
                        if (e instanceof HttpException) {
                            HttpException httpException = (HttpException) e;
                            //httpException.response().errorBody().string()
                            int code = httpException.code();
                            if (code == 500 || code == 404) {
                                onFailure("服务器出错");
                            }
                        } else if (e instanceof ConnectException) {
                            onFailure("网络断开,请打开网络!");
                        } else if (e instanceof SocketTimeoutException) {
                            onFailure("网络连接超时!!");
                        } else {
                            onFailure("发生未知错误" + e.getMessage());
//                            Logger.e("MyFind", e.getMessage());
                        }

                    }

                    @Override
                    public void onNext(ShopBrand_Info info) {
                        if (info.getCode() == 1) {
                            onSuccess(info);//成功------获取完数据,返回给P---P获取到数据之后就将数据交回给V
                        } else {
                            onFailure("没有数据");//失败
                        }
                    }


                });
        try {
            //因为网络请求在子线程，所以不能让宿主线程过早的结束。
            Thread.sleep(100);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }

    /**
     * @param vp2
     * @param nHospitaltabl
     * @param childFragmentManager
     */

    public void setTabCommodity(ViewPager vp2, XTabLayout nHospitaltabl, FragmentManager childFragmentManager) {
        Map<String, String> map = new TreeMap<>();
map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postShopGoodstype(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<ShopGoodstype_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                        onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        //失败的时候调用-----一下可以忽略 直接 callBack.onFaild("请求失败");
                        onFailure("");

                    }

                    @Override
                    public void onNext(ShopGoodstype_Info find_info) {
                        if (find_info.getCode() == 1) {
                            onSuccess(find_info);
                        }
                    }


                });
        try {
            //因为网络请求在子线程，所以不能让宿主线程过早的结束。
            Thread.sleep(100);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }


    }

    /**
     * 设置头部icon
     *
     * @param map2
     */
    public void setIcon(HashMap<String, String> map2) {
        Map<String, String> map = new TreeMap<>();
        map.put("cid", 0 + "");
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postStoreCustomclass(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<StoreCustomclass_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(StoreCustomclass_Info info) {
                        if (info.getCode() == 1) {
                            onSuccess(info);
                        }
                    }
                });

    }


    @OnClick({R.id.nHospital_wddd, R.id.nHome_search, R.id.cornerimg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.nHospital_wddd:
//


                //1.创建一个Dialog对象，如果是AlertDialog对象的话，弹出的自定义布局四周会有一些阴影，效果不好
                Dialog mDialog = new Dialog(getContext());
                //去除标题栏
                mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                //2.填充布局
                LayoutInflater inflater = LayoutInflater.from(getContext());
                View dialogView = inflater.inflate(R.layout.dialog_hosiptal_wddd, null);

                //将自定义布局设置进去
                mDialog.setContentView(dialogView);
                //3.设置指定的宽高,如果不设置的话，弹出的对话框可能不会显示全整个布局，当然在布局中写死宽高也可以
                Window window = mDialog.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                window.setGravity(Gravity.RIGHT | Gravity.TOP);
                lp.x = 20;//设置x坐标
                lp.y = 140;//设置y坐标
                lp.alpha = 0.999f;
                window.setAttributes(lp);
                window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                //                lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
                //                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                //注意要在Dialog show之后，再将宽高属性设置进去，才有效果
                mDialog.show();
                window.setAttributes(lp);
                //设置点击其它地方不让消失弹窗
                mDialog.setCancelable(true);
                //                initDialogView(dialogView);
                //                initDialogListener();

                break;
            case R.id.nHome_search:
                Intent intent = new Intent(getContext(), Hospital_Search_Activity.class);
                startActivity(intent);
                break;
            case R.id.cornerimg:
                int uid = Hawk.get("uid", 0);
                Logger.i("账号id==" + uid);
                if (uid == 0) {
                    Intent intent2 = new Intent(getActivity(), LogIn_Activity.class);
                    startActivity(intent2);
                } else {
                    Intent intent2 = new Intent(getActivity(), ShopCart_Activity.class);
                    startActivity(intent2);
                }

                break;

        }
    }


}
